<template>
  <a-modal
      :title="title"
      :width="800"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
      wrapClassName="ant-modal-cust-warp"
      style="top:5%;height: 85%;overflow-y: hidden">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" v-bind="layout" :model="model" :rules="rules">
        <a-form-model-item label="父级菜单" required prop="pid">
          <a-select show-search
                    option-filter-prop="label"
                    :disabled="formDisabled"
                    v-model="model.pid"
                    placeholder="请选择父级菜单">
            <a-select-option
                v-for="item in menuOptions"
                :key="item.id"
                :value="item.id"
                :label="item.name"
            >
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="菜单类型" required prop="mode">
          <a-select v-model="model.mode" :disabled="formDisabled" placeholder="请选择菜单类型">
            <a-select-option :value="Number(0)">菜单</a-select-option>
            <a-select-option :value="Number(1)">按钮</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item v-if="model.mode === 0" label="菜单路由" required prop="router">
          <a-input v-model="model.router" :disabled="formDisabled" placeholder="请输入菜单路由"/>
        </a-form-model-item>
        <a-form-model-item :label="model.mode === 0? '菜单名称': '按钮名称'" required prop="name">
          <a-input v-model="model.name" :disabled="formDisabled"
                   :placeholder="model.mode === 0? '请输入菜单名称': '请输入按钮名称'"/>
        </a-form-model-item>
        <a-form-model-item v-if="model.mode === 0" label="菜单图标" prop="icon">
          <a-input v-model="model.icon" :disabled="formDisabled" placeholder="请输入菜单图标"/>
        </a-form-model-item>
        <a-form-model-item label="权限编码" require prop="authority">
          <a-input v-model="model.authority" :disabled="formDisabled" placeholder="请输入权限编码"/>
        </a-form-model-item>
        <a-form-model-item v-if="model.mode === 0" label="菜单排序" required prop="sort">
          <a-input-number :min="1" style="width: 100%" :precision="0" v-model="model.sort" :disabled="formDisabled"
                          placeholder="请输入菜单序号"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import {save, list as menuList} from '@/services/menu'

export default {
  name: "MenuForm",
  data() {
    return {
      title: "操作",
      visible: false,
      formDisabled: false,
      isOkDisabled: false,
      model: {},
      menuOptions: [],
      layout: {
        labelCol: {span: 3},
        wrapperCol: {span: 20},
      },
      confirmLoading: false,
      rules: {
        router: [
          {required: true, message: '请输入菜单路由!'},
        ],
        name: [
          {required: true, message: '请输入菜单名称!'},
        ],
        sort: [
          {required: true, message: '请输入菜单序号!'},
        ],
        authority: [
          {required: true, message: '请输入权限编码!'},
        ]
      },
    }
  },
  mounted() {
    this.initDict()
  },
  methods: {
    add() {
      this.title = "添加菜单";
      this.model = {};
      this.formDisabled = false
      this.isOkDisabled = false
      this.visible = true;
    },
    edit(record) {
      this.title = "编辑菜单";
      this.model = Object.assign({}, record);
      this.formDisabled = false
      this.isOkDisabled = false
      this.visible = true;
    },
    view(record) {
      this.title = "查看菜单";
      this.model = Object.assign({}, record);
      this.formDisabled = true
      this.isOkDisabled = true
      this.visible = true;
    },
    submit() {
      if (this.isOkDisabled) {
        this.visible = false;
      } else {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.confirmLoading = true;
            save(this.model).then(res => {
              if (res.data.code === 200) {
                this.$message.success('操作成功' || res.data.msg);
                this.$emit('refresh');
              } else {
                this.$message.error('操作失败' || res.data.msg);
              }
            }).catch(error => {
              this.$message.error('操作失败' || error.message);
            }).finally(() => {
              this.confirmLoading = false;
            })
            this.visible = false;
          }
        })
      }
    },
    close() {
      this.$refs.form.clearValidate();
      this.$emit('close');
      this.visible = false;
      this.model = {}
    },
    handleOk() {
      this.submit()
    },
    handleCancel() {
      this.close()
    },
    initDict() {
      menuList({}).then(res => {
        if (res.data.code === 200) {
          this.menuOptions = res.data.data
        } else {
          this.menuOptions = []
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
